﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace StoreWeb.system.order
{
    public partial class sendlist : System.Web.UI.Page
    {
        Maticsoft.BLL.Orders bll = new Maticsoft.BLL.Orders();
        Maticsoft.BLL.UA_Account uabll = new Maticsoft.BLL.UA_Account();
        BLL.DealersManage dmbll = new BLL.DealersManage();
        Maticsoft222.BLL.sqlexec sqlec = new Maticsoft222.BLL.sqlexec();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                hidwhere.Value = "orderstatus=7";//已入库
                ddliststore.DataSource = dmbll.GetList("userstate='已审核'");
                ddliststore.DataValueField = "id";
                ddliststore.DataTextField = "username";
                ddliststore.DataBind();
                ddliststore.Items.Insert(0, new ListItem("--根据门店查询--", "0"));
                bindData(hidwhere.Value);
            }
        }
        void bindData(string where)
        {
            //Repeater1.DataSource = bll.GetList(where);
            //Repeater1.DataBind();
            Repeater1.DataSource = sqlec.query("select distinct  b.id,b.username from dbo.Orders a left join DealersManage b on a.DealersManageID = b.id where " + where);
            Repeater1.DataBind();
        }

        protected void btnserach_Click(object sender, EventArgs e)
        {
            string dmwhere = "orderstatus=7";
            string where = " orderstatus=7 ";
            if (ddliststore.SelectedItem.Value != "0")
            {
                dmwhere += " and b.id='" + ddliststore.SelectedItem.Value+"'";
                where += " and DealersManageID='" + ddliststore.SelectedItem.Value + "'";
            }
            if (!string.IsNullOrEmpty(startTime.Value) && !string.IsNullOrEmpty(endTime.Value))
            {
                where += " and createtime between '" + startTime.Value + "' and '" + endTime.Value + "'";
            }
            if (!string.IsNullOrEmpty(mobile.Value))
            {
                where += " and membermobile like '%" + mobile.Value + "%'";
            }
            if (!string.IsNullOrEmpty(code.Value))
            {
                where += " and code like '%" + code.Value + "%'";
            }
            hidwhere.Value = where;
            bindData(dmwhere);
        }
        //今天
        protected void btnday_Click(object sender, EventArgs e)
        {
            hidwhere.Value = string.Format("createtime between '{0}' and '{1}'", DateTime.Now.ToString("yyyy-MM-dd 00:00:00"), DateTime.Now.ToString("yyyy-MM-dd 23:59:59"));
            bindData("orderstatus=7");
        }
        //本周
        protected void btnweeks_Click(object sender, EventArgs e)
        {
            int weeknow = Convert.ToInt32(System.DateTime.Now.DayOfWeek);
            //星期日 获取weeknow为0  
            weeknow = weeknow == 0 ? 7 : weeknow;
            int daydiff = (-1) * weeknow + 1;
            int dayadd = 7 - weeknow;
            hidwhere.Value = string.Format("createtime between '{0}' and '{1}'", System.DateTime.Now.AddDays(daydiff).ToString("yyyy-MM-dd 00:00:00"), System.DateTime.Now.AddDays(dayadd).ToString("yyyy-MM-dd 23:59:59"));
            bindData("orderstatus=7");
        }
        //本月
        protected void btnmonth_Click(object sender, EventArgs e)
        {
            DateTime dt = DateTime.Now;
            //本月第一天时间   
            DateTime dt_First = dt.AddDays(-(dt.Day) + 1);
            //将本月月数+1   
            DateTime dt2 = dt.AddMonths(1);
            DateTime dt_Last = dt2.AddDays(-(dt.Day));
            hidwhere.Value = string.Format("createtime between '{0}' and '{1}'", dt_First.ToString("yyyy-MM-dd 00:00:00"), dt_Last.ToString("yyyy-MM-dd 23:59:59"));
            bindData("orderstatus=7");
        }
        //最近三月
        protected void btntree_Click(object sender, EventArgs e)
        {
            DateTime dt = DateTime.Now;
            //本月第一天时间   
            DateTime dt_First = dt.AddDays(-(dt.Day) + 1);
            //将本月月数+1   
            DateTime dt2 = dt.AddMonths(3);
            DateTime dt_Last = dt2.AddDays(-(dt.Day));
            hidwhere.Value = string.Format("createtime between '{0}' and '{1}'", dt_First.ToString("yyyy-MM-dd 00:00:00"), dt_Last.ToString("yyyy-MM-dd 23:59:59"));
            bindData("orderstatus=7");
        }

        protected void ddliststore_SelectedIndexChanged(object sender, EventArgs e)
        {
            string where = "orderstatus=7";
            if (ddliststore.SelectedItem.Value != "0")
            {
                where += " and b.id='" + ddliststore.SelectedItem.Value + "'";
            }
            bindData(where);
        }

        protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                HiddenField hfid = e.Item.FindControl("hidid") as HiddenField;
                Repeater rpt = e.Item.FindControl("Repeater2") as Repeater;
                rpt.DataSource = bll.GetList(hidwhere.Value + " and orderstatus=7 and DealersManageID='" + hfid.Value + "'");
                rpt.DataBind();
            }
        }
    }
}